import java.util.*;

import javax.microedition.lcdui.*;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
 * MIDlet which functionality is to compute the shortest route 
 * between two different stops at a public transportation system
 * @author Paula Gonz�lez Mu�oz
 * @author Alex Bruck
 * @author Cem Altel
 */

public class GUI extends MIDlet
{
	/**
	 * Display where the information will be shown
	 */
	Display display = null;
	// map engine
	
	/**
	 * 
	 */
	MapEngine engine = null;
	

	/**
	 * Main menu
	 */
	List menu = null;
	
	/**
	 * Menu that will allow the user to access to the origin and destination choice screens
	 */
	List trip = null;
	
	/**
	 * list of possible stops to select from
	 */
	List stops = null;
	
	/**
	 * List of stops that compose the shortest route between {@link #origin origin} and {@link #destination destination}
	 */
	List result = null;
	
	Form error = null;
	
	/**
	 * Command that allows the user to navigate backward
	 */
	static final Command backCommand = new Command("Back", Command.BACK, 0);
	
	/**
	 * Command that allows the user to navigate forward
	 */
	static final Command okCommand = new Command("OK", Command.OK, 0);
	
	/**
	 * Command that allows the user to compute the shortest route
	 */
	static final Command computeCommand = new Command("Calculate", Command.OK, 0);
	
	/**
	 * Command that allows the user to exit the application
	 */
	static final Command exitCommand = new Command("Exit", Command.STOP, 0);
	

	/**
	 * Handler for the main menu screen events
	 */
	Listener listener;
	
	/**
	 * Handler for the new travel screen events
	 */
	TripListener tripListener;
	
	/**
	 * Handler for the Origin selection screen events
	 */
	OriginListener originListener;
	
	/**
	 * Handler for the Destination selection screen events
	 */
	DestinationListener destinationListener;
	
	/**
	 * Handler for the results screen events
	 */
	ResultListener resultListener;
	
	MapEngine mapEngine = null;
	
	String currentMenu=null;
	

	/**
	 * Origin stop
	 */
	String origin = null;
	
	/**
	 * Destination stop 
	 */
	String destination = null;
	
	/**
	 * Vector that contains all the possible stops to chose from.
	 */
	Vector list_stops = null;
	
	StringBuffer storage;
	
	
	Alert welcome = null;
	
	public GUI()
	{
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
	 */
	public void destroyApp(boolean arg0) throws MIDletStateChangeException
	{
		notifyDestroyed();
	}

	/* (non-Javadoc)
	 * @see javax.microedition.midlet.MIDlet#pauseApp()
	 */
	public void pauseApp()
	{
		display = null;
		menu = null;
	}

	/* (non-Javadoc)
	 * @see javax.microedition.midlet.MIDlet#startApp()
	 * Method used for initializing the MIDlet components.
	 */
	public void startApp() throws MIDletStateChangeException
	{
		display = Display.getDisplay(this);
		listener = new Listener(this);
		tripListener = new TripListener(this);
		originListener = new OriginListener(this);
		destinationListener = new DestinationListener(this);
		resultListener = new ResultListener(this);
		
		storage = new StringBuffer();
		
		origin = new String("");
		destination = new String("");
		
		menu = new List ("Menu", Choice.IMPLICIT);
		menu.append("New Travel", null);
		menu.append("Upload new map", null);
		
		menu.addCommand(exitCommand);
		menu.addCommand(okCommand);
		menu.setCommandListener(listener);
		
		startUp();
		mainMenu();
	}
	
	/**
	 * Method used for showing a welcome message and initializing the map engine
	 */
	public void startUp()
	{
		// it doesn't stay for five seconds
		int timeout = Alert.FOREVER;
		welcome = new Alert("Journey Planner", "Welcome to the CPA's Journey Planner.\n Press a button.", null, null);
		welcome.setTimeout(timeout);
		welcome.setType(AlertType.ALARM);
		
		display.setCurrent(welcome);
		
		
		
		//we need the graph for the MapEngine
		mapEngine =new MapEngine(null, display);
	}
	
	/**
	 * Method that shows on the current display the main menu
	 */
	public void mainMenu()
	{
		//every time we go to the main menu the origin and destination must reset
		origin = "";
		destination = "";
		display.setCurrent(menu);
		currentMenu = "Main";
	}
	
	/**
	 * Method that generate the From/To screen and gives the option to compute the shortest path
	 */
	public void newTravel()
	{
		
		trip = new List("Journey Planner", Choice.IMPLICIT);
		
		
		//if origin or destination are not reseted, they should show our choice here
		trip.append("From: "+origin, null);
		trip.append("To: "+destination, null);
		
		trip.addCommand(backCommand);
		trip.addCommand(computeCommand);
		
		trip.setCommandListener(tripListener);
		
		display.setCurrent(trip);
		currentMenu = "Journey Planner";
	}
	
	/**
	 * Method that allows the user to chose their origin stop from a list of all possible stops
	 */
	public void setOrigin()
	{
		stops = new List("Select origin", Choice.IMPLICIT);
		
		//we need to somehow get all the stops sorted alphabetically
		list_stops = new Vector();
		
		/*
		 *Test lines while we don't have the complete list of stops 
		 */
		if(list_stops.size()==0)
		{
			stops.append("Under construction", null);
		}
		/*
		 * end of test
		 */
		
		for(int i=0; i < list_stops.size(); i++)
		{
			
			String actual_stop = (String) list_stops.elementAt(i);
			stops.append(actual_stop, null);
		}
		stops.addCommand(backCommand);
		stops.addCommand(okCommand);
		stops.setCommandListener(originListener);
		display.setCurrent(stops);
		currentMenu = "Select origin";
		
	}
	
	/**
	 * Method that allows the user to chose their destination stop from a list of all possible stops
	 */
	public void setDestination()
	{
		stops = new List("Select destination", Choice.IMPLICIT);
		String actual_stop;
		//we need to somehow get all the stops
		list_stops = new Vector();
		/*
		 *Test lines while we don't have the complete list of stops 
		 */
		if(list_stops.size()==0)
		{
			stops.append("Under construction", null);
		}
		/*
		 * end of test
		 */
		for(int i=0; i < list_stops.size(); i++)
		{			
			actual_stop = (String) list_stops.elementAt(i);
			stops.append(actual_stop, null);
		}
		
		stops.addCommand(backCommand);
		stops.addCommand(okCommand);
		stops.setCommandListener(destinationListener);
		display.setCurrent(stops);
		currentMenu = "Select destination";
	}
	
	
	/**
	 * Method that shows the resulting path
	 * @param path contains the list of stops that form the shortest route
	 */
	public void showResult(Vector path)
	{
		result = new List("Path", Choice.IMPLICIT );
		String stop;
		for(int i = 0; i < path.size(); i++)
		{
			
			stop = (String) path.elementAt(i);
			result.append(stop, null);
				
		}
		result.addCommand(backCommand);
		result.addCommand(exitCommand);
		result.setCommandListener(resultListener);
		display.setCurrent(result);
		currentMenu = "Path";
	}
	
	public void readRecords(Vector records)
	{
		String aux;
		 
		for (int i = 0; i < records.size();i++)
		{
			aux=(String) records.elementAt(i);
			storage.append(aux);
		}
	}
	
}
